package com.example.demo.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.CasesMapper;
import com.example.demo.mapper.CasesTypeMapper;
import com.example.demo.pojo.Cases;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/front/cases")
public class FrontCasesController {

    @Resource
    private CasesMapper casesMapper;
    @Resource
    private CasesTypeMapper casesTypeMapper;

    @RequestMapping("/add")
    public Object add(@RequestBody Cases cases){
        cases.setCasesStatus("待审核");
        cases.setCasesCreateTime(new Date());
        if(cases.getCasesIntroduce().length() > 50){
            cases.setCasesSimpleIntroduce(cases.getCasesIntroduce().substring(0,45));
        }
        cases.setCasesSimpleIntroduce(cases.getCasesIntroduce());
        casesMapper.insert(cases);
        return LayuiResult.LayuiData(0,"案件已提交，请耐心等待！",null);
    }

    @RequestMapping("/getAll")
    public Object getAllComplete(){
        QueryWrapper<Cases> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).eq("cases_status","案件已审结");
        List<Cases> cases = casesMapper.selectList(queryWrapper);
        return LayuiResult.selectSuccess(cases);
    }

    @RequestMapping("/getFour")
    public Object getFourComplete(){
        QueryWrapper<Cases> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).eq("cases_status","案件已审结");
        List<Cases> cases = casesMapper.selectList(queryWrapper);
        cases.forEach(cases1 -> {
            cases1.setCasesTypeName(casesTypeMapper.selectById(cases1.getCasesTypeId()).getTypeName());
        });
        if(cases.size() > 4){
            cases = cases.subList(0,4);
        }
        return LayuiResult.selectSuccess(cases);
    }

    @RequestMapping("/getByIdCard/{idCard}")
    public Object getByIdCard(@PathVariable String idCard,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<Cases> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).eq("cases_plaintiff_id_card",idCard);
        List<Cases> cases = casesMapper.selectList(queryWrapper);
        Integer count = casesMapper.selectCount(queryWrapper);
        PageInfo<Cases> pageInfo = new PageInfo<>(cases);
        pageInfo.getList().forEach(cases1 -> {
            cases1.setCasesTypeName(casesTypeMapper.selectById(cases1.getCasesTypeId()).getTypeName());
        });
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }

    @RequestMapping("/getById/{id}")
    public Object getById(@PathVariable Integer id){
        Cases cases = casesMapper.selectById(id);
        return LayuiResult.selectSuccess(cases);
    }


}
